package com.ruoyi.tranShort.mqtt.request;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.mqtt.api.MqttClientApi;
import com.ruoyi.tranShort.mqtt.ResponseHandle;
import com.ruoyi.tranShort.mqtt.api.GatewayRequest;
import com.ruoyi.tranShort.mqtt.api.GatewayResponse;
import com.ruoyi.tranShort.mqtt.common.constants.ResponseConstants;
import com.ruoyi.tranShort.mqtt.common.constants.TypeConstants;
import com.ruoyi.tranShort.mqtt.entity.ComType;
import com.ruoyi.tranShort.mqtt.entity.Content;
import com.ruoyi.tranShort.mqtt.entity.RequestBase;
import com.ruoyi.tranShort.mqtt.exception.ResponseTimeOutException;
import com.ruoyi.tranShort.mqtt.request.entity.CloseDeviceData;
import com.ruoyi.tranShort.mqtt.request.entity.ConnectDeviceLongData;
import com.ruoyi.tranShort.mqtt.response.CloseDeviceResponse;
import com.ruoyi.tranShort.utils.MqttClientUtil;

/**
 * 关闭连接
 * @author wwy
 * @date 2021-08-11 13:55
 */
public class CloseDeviceRequest implements GatewayRequest {
    private RequestBase requestBase = new RequestBase();
    private ComType comType = new ComType();
    private Content content = new Content();
    public CloseDeviceRequest(){
        requestBase.setComType(comType);
        comType.setClientId("");
        comType.setMsgId(System.currentTimeMillis()+"");
        comType.setType(TypeConstants.DOWN);
        comType.setContent(content);
        content.setType(TypeConstants.DISCONNECT_DEVICE);

    }
    private CloseDeviceData closeDeviceData;
    private String pubTopic;
    private int qos;
    @Override
    public CloseDeviceResponse execute() {
        CloseDeviceResponse closeDeviceResponse = new CloseDeviceResponse();
        closeDeviceResponse.setCode(500);
        if(closeDeviceData != null){
            content.setData(closeDeviceData);
            //发送消息
            MqttClientApi mqttClientApi = MqttClientUtil.getMqttClientApi();
            String message = JSONObject.toJSONString(requestBase);
            mqttClientApi.publishMessage(pubTopic,message,qos);
            //监听上行消息，判断是否成功
            String mac = closeDeviceData.getMac();
            try {
                String waitMessage = ResponseHandle.waitCloseMessage(mac.toUpperCase());
                if(ResponseConstants.OK.equals(waitMessage)){
                    closeDeviceResponse.setCode(200);
                }else{
                    closeDeviceResponse.setCode(500);
                }

            } catch (ResponseTimeOutException e) {
                //超时
                closeDeviceResponse.setCode(500);
            }
        }


        return closeDeviceResponse;
    }

    public CloseDeviceData getCloseDeviceData() {
        return closeDeviceData;
    }

    public void setCloseDeviceData(CloseDeviceData closeDeviceData) {
        this.closeDeviceData = closeDeviceData;
    }

    public String getPubTopic() {
        return pubTopic;
    }

    public void setPubTopic(String pubTopic) {
        this.pubTopic = pubTopic;
    }

    public int getQos() {
        return qos;
    }

    public void setQos(int qos) {
        this.qos = qos;
    }
}
